exports.addCustomer = (req, res) => {

    var bodyParams = req.routeInfo.bodyParams;

    var resultData = {};

    var openid = [];

    var userInformation = 0;

    var fatherEnterprise = 0;

    //添加用户
    function AddCustomer(cb) {

        var addParam = {
            c_address: bodyParams.c_address,
            c_area: bodyParams.c_area,
            c_area_name: bodyParams.c_area_name,
            c_area_of: bodyParams.c_area_of,
            c_campus_name: bodyParams.c_campus_name,
            c_city: bodyParams.c_city,
            c_city_name: bodyParams.c_city_name,
            c_create_time: bodyParams.c_create_time,
            c_evaluation: bodyParams.c_evaluation,
            c_is_kp: bodyParams.c_is_kp,
            c_name: bodyParams.c_name,
            c_note: bodyParams.c_note,
            c_openid: bodyParams.c_openid,
            c_osid: bodyParams.c_osid,
            c_phone: bodyParams.c_phone,
            c_position: bodyParams.c_position,
            c_provinces: bodyParams.c_provinces,
            c_provinces_name: bodyParams.c_provinces_name,
            c_student_count: bodyParams.c_student_count,
            c_teach_type: bodyParams.c_teach_type,
            c_uid: bodyParams.c_uid,
            c_weixin_nickname: bodyParams.c_weixin_nickname,
            c_pca_code: bodyParams.c_pca_code,
            c_teach_type_text: bodyParams.c_teach_type_text,
            c_campus_start_time: bodyParams.c_campus_start_time,
            c_campus_now_system: bodyParams.c_campus_now_system,
        };

        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.artCustomerAdd.Request_Api_Url,
                method: 'POST',
                data: addParam
            },
            function (data) {

                resultData.addResult = data.result;
                cb();

            }
        )
    }

    /*发私信 sendPrivateMessage */

    //查询销售
    function searchSaleInfo(cb) {
        if(bodyParams.u_leader == 0){
            return cb();
        }
        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url + "/u_is_delete/0/u_id/" + bodyParams.u_leader,
                method: 'POST',
                data: {
                    fields: ['u_master_osid', 'u_is_manager', 'u_openid'].join(',')
                }
            },
            function (data) {

                openid.push(data.result.data[0].u_openid);

                cb();

            }
        )
    }

    //查询代理
    function searchAgent(cb) {
        if(bodyParams.u_leader == 0){
            return cb();
        }
        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url + "/u_is_delete/0/u_type/2/u_master_osid/" + bodyParams.c_osid,
                method: 'POST',
                data: {
                    fields: ['u_master_osid', 'u_is_manager', 'u_openid'].join(',')
                }
            },
            function (data) {

                openid.push(data.result.data[0].u_openid);

                cb();

            }
        )
    }

    //发私信
    function sendPrivateMessage(cb) {

        F.SerialEach(openid, function (item, index, callback) {

            console.log("---------------");
            console.log(item);

            function loopSend(eachCb){
                if(item == 0 || item == ''){
                    eachCb();
                }else{
                    //新增客提醒
                    var params = {
                        type : "xinKeHu",
                        openid : item,
                        goUrls : "",//跳转
                        customerId : resultData.addResult,//新用户ID
                        name : bodyParams.c_name + '('+bodyParams.c_campus_name+')', //客户名
                        sellName : bodyParams.sell_name, //销售名
                    };

                    console.log(config.server.ip + ":" + config.server.port + CORE_APIConfig.coreAPI.templateMessagesSent.Request_Api_Name)

                    R.SEND_HTTP(
                        req,  
                        res,
                        {
                            url: config.server.ip + ":" + config.server.port + CORE_APIConfig.coreAPI.templateMessagesSent.Request_Api_Name,
                            method: 'POST',
                            data: params
                        },
                        function (data) {
                            console.log(data);
                            eachCb();

                        }
                    )
                }
            }

            F.Parallel([
                loopSend
            ], () => {
                callback();
            })

        }, cb)

    }

    F.Serial([
        AddCustomer,
        searchSaleInfo,
        searchAgent,
        sendPrivateMessage
    ], () => {
        RES.successResponse(res, resultData);
    })


}